import sys
import os
import base64

if __name__ == '__main__':
    argc = len(sys.argv)
    if argc != 2 and argc != 3:
        raise ValueError('1 or 2 argument(s) is(are) needed, but %d is given.' % (argc-1))
    img_path = sys.argv[1]

    if not os.path.exists(img_path):
        raise ValueError('File "%s" dose not exist.' % img_path)
    with open(img_path, 'rb') as f:
        image = f.read()
        image_base64 = str(base64.b64encode(image), encoding='utf-8')
    img_name = img_path.split('/')[1] + '.base64'
    if argc == 3:
        file_path = sys.argv[2]
    else:
        file_path = './%s.txt' % img_name
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(image_base64)
